package com.adv.ems.utils;

import cn.hutool.json.JSONUtil;
import com.adv.ems.constants.SystemConst;
import com.adv.ems.model.dto.NameInfoDTO;
import com.adv.ems.model.dto.SatelliteLocationDTO;
import org.springframework.util.StringUtils;
import java.text.DecimalFormat;
import java.time.Duration;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

public class EmsUtil {
    private static final DecimalFormat dataFormat = new DecimalFormat("#.##");

    public static String valueFormat(Double value) {
        if(value == null){
            return "";
        }
        return dataFormat.format(value);
    }

    public static <T> void getAbsolutePath(Long id, StringBuilder builder, Map<Long, T> idToMap, Function<T, Long> getParentId, Function<T, String> getPath) {
        T t = idToMap.get(id);
        if(t == null){
            return;
        }
        String path = getPath.apply(t);
        Long parentId = getParentId.apply(t);
        if (parentId == null) {
            return;
        }
        if (!StringUtils.isEmpty(path)) {
            builder.reverse();
            builder.append(path);
            return;
        }
        T parentObj = idToMap.get(parentId);
        if (parentObj == null) {
            return;
        }
        String parentPath = getPath.apply(parentObj);
        if (!StringUtils.isEmpty(parentPath)) {
            builder.reverse();
            builder.append(parentPath).append(parentId + "#");
            return;
        }
        getAbsolutePath(parentId, builder, idToMap, getParentId, getPath);
        if (builder.length() == 0) {
            builder.append("#" + parentId + "#");
        }
    }

    public static SatelliteLocationDTO formatLocation(String satelliteLocation) {
        if (StringUtils.isEmpty(satelliteLocation)) {
            return new SatelliteLocationDTO();
        }
        try {
            return JSONUtil.toBean(satelliteLocation, SatelliteLocationDTO.class);
        }catch (Exception e) {
            e.printStackTrace();
        }
        return new SatelliteLocationDTO();
    }
    public static Boolean isBelongChildCityAreaCode(String parentCode, String childCode) {
        if (parentCode == null || childCode == null) {
            return false;
        }
        String parentNode = parentCode.replaceAll("[0]*$", "");
        return childCode.indexOf(parentNode) == 0;
    }

    public static NameInfoDTO getNameInfo(String nameInfo, String acceptLanguage) {
        if (StringUtils.isEmpty(nameInfo)) {
            return new NameInfoDTO();
        }
        NameInfoDTO bean = JSONUtil.toBean(nameInfo, NameInfoDTO.class);
        if (!StringUtils.isEmpty(bean.getFullName())) {
            bean.setFullName(multilingual(bean.getFullName(), acceptLanguage));
        }
        if (!StringUtils.isEmpty(bean.getShortName())) {
            bean.setShortName(multilingual(bean.getShortName(), acceptLanguage));
        }
        return bean;
    }

    public static String multilingual(String languageInfo, String acceptLanguage) {
        if (StringUtils.isEmpty(languageInfo)) {
            return "";
        }
        acceptLanguage = StringUtils.isEmpty(acceptLanguage) ? SystemConst.Header.ZH_CN : acceptLanguage;
        Map<Object, Object> bean = JSONUtil.toBean(languageInfo, Map.class);
        return bean.getOrDefault(acceptLanguage, "").toString();
    }

    public static Map.Entry<Boolean, String> getDeviceState(Date last) {
        boolean health = true;
        String stateDesc = "服务中";
        if (last == null) {
            health = false;
            stateDesc = "从未接收数据";
        }else {
            long dis = System.currentTimeMillis() - last.getTime();
            if (dis > Duration.ofDays(30L).toMillis()) {
                stateDesc = "超过一个月未接收数据";
                health = false;
            }else if (dis > Duration.ofDays(1L).toMillis()) {
                stateDesc = "超过一天未接收数据";
                health = false;
            }else if (dis > Duration.ofHours(1L).toMillis()) {
                stateDesc = "超过一小时未接收数据";
                health = false;
            }
        }
        Map<Boolean, String> rs = new HashMap<>(1);
        rs.put(health, stateDesc);
        return rs.entrySet().iterator().next();
    }
}
